<template>
  <div class="electricity-container">
    <!-- Electric Energy Storage -->
     <div class="storage-section">
      <button class="distribution-btn emission-submit-btn" @click="submitAllDeviceParams">
        <span class="btn-text">提交设备参数</span>

      </button>
      <h2 class="title">电储能</h2>
      <div class="info-grid">
        <div class="info-card">
          <label class="info-label">故障率</label>
          <div class="info-content">
            <input type="number" v-model="Storage1" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">故障修复时间(h)</label>
          <div class="info-content">
            <input type="number" v-model="Storage2" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>
      </div>
    </div>

      <div class="storage-section">
        <h2 class="title">热储能</h2>
        <div class="info-grid">
          <div class="info-card">
            <label class="info-label">故障率</label>
            <div class="info-content">
              <input type="number" v-model="Storage3" class="form-input small">
              <span class="info-value"></span>
            </div>
          </div>

          <div class="info-card">
            <label class="info-label">故障修复时间(h)</label>
            <div class="info-content">
              <input type="number" v-model="Storage4" class="form-input small">
              <span class="info-value"></span>
            </div>
          </div>
        </div>
      </div>

    <div class="storage-section">
      <h2 class="title">冷储能</h2>
      <div class="info-grid">
        <div class="info-card">
          <label class="info-label">故障率</label>
          <div class="info-content">
            <input type="number" v-model="Storage5" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">故障修复时间(h)</label>
          <div class="info-content">
            <input type="number" v-model="Storage6" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>
      </div>
    </div>

      <div class="storage-section">
        <h2 class="title">氢储能</h2>
        <div class="info-grid">
          <div class="info-card">
            <label class="info-label">故障率</label>
            <div class="info-content">
              <input type="number" v-model="Storage7" class="form-input small">
              <span class="info-value"></span>
            </div>
          </div>

          <div class="info-card">
            <label class="info-label">故障修复时间(h)</label>
            <div class="info-content">
              <input type="number" v-model="Storage8" class="form-input small">
              <span class="info-value"></span>
            </div>
          </div>
        </div>


  </div>

</div>

</template>

<script lang="ts" setup>
import { ref, onMounted, type Ref } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const submitAllDeviceParams = async () => {
  try {
    const payloads = [
  { id: 1, value: parseFloat(Storage1.value) || 0, url: 'diancn' },
  { id: 2, value: parseFloat(Storage2.value) || 0, url: 'diancn' },
  { id: 3, value: parseFloat(Storage3.value) || 0, url: 'recn' },
  { id: 4, value: parseFloat(Storage4.value) || 0, url: 'recn' },
  { id: 5, value: parseFloat(Storage5.value) || 0, url: 'lengcn' },
  { id: 6, value: parseFloat(Storage6.value) || 0, url: 'lengcn' },
  { id: 7, value: parseFloat(Storage7.value) || 0, url: 'qingcn' },
  { id: 8, value: parseFloat(Storage8.value) || 0, url: 'qingcn' },

    ]

    for (const item of payloads) {
      await axios.put(`http://localhost:8080/${item.url}`, {
        id: item.id,
        value: item.value
      })
    }

    ElMessage.success('所有设备参数提交成功')
  } catch (err) {
    ElMessage.error('提交失败，请检查网络或数据')
    console.error(err)
  }
}

const fetchAndSet = async (url: string, variable: Ref<string>) => {
  try {
    const response = await axios.get(url);
    variable.value = response.data.value;
  } catch  {
    ElMessage.error(`加载 ${url} 失败`);
  }
};

onMounted(async () => {

    await fetchAndSet('http://localhost:8080/diancn/9', Storage1);
    await fetchAndSet('http://localhost:8080/diancn/10', Storage2);
    await fetchAndSet('http://localhost:8080/recn/9', Storage3);
    await fetchAndSet('http://localhost:8080/recn/10', Storage4);
    await fetchAndSet('http://localhost:8080/lengcn/9', Storage5);
    await fetchAndSet('http://localhost:8080/lengcn/10', Storage6);
    await fetchAndSet('http://localhost:8080/qingcn/9', Storage7);
    await fetchAndSet('http://localhost:8080/qingcn/10', Storage8);
})


// Hydrogen Storage
    const Storage1 = ref('')
    const Storage2 = ref('')
    const Storage3 = ref('')
    const Storage4 = ref('')
    const Storage5 = ref('')
    const Storage6 = ref('')
    const Storage7 = ref('')
    const Storage8 = ref('')

</script>

<style scoped>
.submit-emission-container {
  text-align: center;
  margin-top: 24px;
}

.emission-submit-btn {
  background-color: #7c3aed; /* 绿色 */
  padding: 12px 24px;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  box-shadow: 0 4px 10px rgba(22, 163, 74, 0.2);
}

.emission-submit-btn:hover {
  background-color: #7c3aed;
  box-shadow: 0 6px 16px rgba(22, 163, 74, 0.3);
}
.electricity-container {
  background-color: #f8fafc;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  border-radius: 14px;
  padding: 36px;
  max-width: 920px;
  margin: 0 auto;
  font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
  color: #1e293b;
}

.title {
  color: #4c1d95;
  text-align: center;
  margin-bottom: 32px;
  font-size: 28px;
  font-weight: 700;
  padding-bottom: 16px;
  border-bottom: 2px solid #7c3aed;
  letter-spacing: 0.5px;
}

.storage-section {
  background-color: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 28px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 18px;
}

.info-card {
  background-color: #f8fafc;
  padding: 18px;
  border-radius: 10px;
  border-left: 4px solid #7c3aed;
  transition: transform 0.2s, box-shadow 0.2s;
}

.info-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.info-label {
  display: block;
  font-weight: 500;
  color: #475569;
  margin-bottom: 10px;
  font-size: 15px;
}

.info-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.form-input {
  padding: 10px 16px;
  border: 1px solid #053676;
  border-radius: 6px;
  width: 120px;
  text-align: right;
  font-size: 15px;
  background: transparent;
  color: #1e293b;
  border: 1px solid #053676;
}

.form-input.small {
  width: 100px;
}

.form-input:focus {
  outline: none;
  box-shadow: 0 0 0 2px rgba(124, 58, 237, 0.2);
}

.info-value {
  font-weight: 600;
  color: #4c1d95;
  font-size: 15px;
  white-space: nowrap;
}

.save-btn {
  background-color: #7c3aed;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s;
  margin-top: 20px;
}

.save-btn:hover {
  background-color: #6d28d9;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(124, 58, 237, 0.3);
}

@media (max-width: 768px) {
  .electricity-container {
    padding: 24px;
    border-radius: 0;
  }

  .info-grid {
    grid-template-columns: 1fr;
  }

  .form-input {
    width: 100%;
  }
}
</style>
